import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/gen/assets.gen.dart';

class SignalView extends StatelessWidget {
  final BleManager? bleManager;
  final bool showDeviceId;

  const SignalView({super.key, this.bleManager, this.showDeviceId = false});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<BluetoothConnectionState>(
        initialData: bleManager?.connectionState.value,
        stream: bleManager?.connectionState.stream,
        builder: (context, snapshot) {
          final connectionState =
              snapshot.data ?? BluetoothConnectionState.disconnected;
          return Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Visibility(
                  visible:
                      connectionState == BluetoothConnectionState.disconnected,
                  child: Image.asset(Assets.image.iconUnconnect.path,
                      width: 16, height: 13)),
              Visibility(
                visible: connectionState == BluetoothConnectionState.connecting,
                child: Image.asset(Assets.image.iconConnecting.path,
                        width: 16, height: 13)
                    .animate(onPlay: (controller) => controller.repeat())
                    .rotate(duration: Duration(seconds: 1)),
              ),
              Visibility(
                  visible:
                      connectionState == BluetoothConnectionState.connected,
                  child: StreamBuilder<num?>(
                      stream: bleManager?.rssi.stream,
                      builder: (context, snapshot) {
                        return buildRssiImage(snapshot.data);
                      })),
              Text(
                connectionState == BluetoothConnectionState.connected
                    ? '已连接'.tr
                    : connectionState == BluetoothConnectionState.disconnected
                        ? '已断开'.tr
                        : '连接中'.tr,
                style: TextStyle(fontSize: 12, color: Colors.white),
              ).marginOnly(top: 4),
              Visibility(
                visible: showDeviceId,
                child: Text(
                  "ID:${bleManager?.deviceModel?.deviceID ?? ''}",
                  style: TextStyle(fontSize: 12, color: Colors.white),
                ).marginOnly(top: 4),
              ),
            ],
          );
        });
  }

  Widget buildRssiImage(num? rssi) {
    // print("rssi:${deviceModel.rssi}");
    if (rssi == null) {
      return Assets.image.icRssi0Bar.image(width: 16, height: 13);
    }
    final rssiValue = 100.0 * (127.0 + (rssi ?? 0)) / (127.0 + 20.0);

    if (rssiValue <= 10) {
      return Assets.image.icRssi0Bar.image(width: 16, height: 13);
    } else if (rssiValue <= 25) {
      return Assets.image.icRssi1WhiteBar.image(width: 16, height: 13);
    } else if (rssiValue <= 48) {
      return Assets.image.icRssi2WhiteBar.image(width: 16, height: 13);
    } else if (rssiValue <= 100) {
      return Assets.image.icRssi3WhiteBar.image(width: 16, height: 13);
    } else {
      return Assets.image.icRssi3WhiteBar.image();
    }
  }
}
